package com.kerrykidz.scmstock.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.kerrykidz.common.Constants;
import com.kerrykidz.common.PrimaryKeyGenerator;
import com.kerrykidz.common.PrimaryKeyGenerator.BusinessType;
import com.kerrykidz.common.service.ICommonService;
import com.kerrykidz.scminvoicing.dao.model.ScmTakeStkEntity;
import com.kerrykidz.scmstock.service.IStockTakingService;
import com.kerrykidz.system.dao.model.GblSecBranEntity;
import com.kerrykidz.system.util.UserUtil;
import com.kerrykidz.system.vo.UserInfo;
import com.rdp.framework.exception.RDPException;

/**
 * 
 * @ClassName: StockTakingController
 * @description: 出库盘点 控制器
 * @author: huanjiang
 * @date: 2014年9月4日 下午3:02:37
 * @version: V1.0
 * 
 */
@Controller
@RequestMapping("StockTaking")
public class StockTakingController {

	@Autowired
	private IStockTakingService iStockTakingService;

	@Autowired
	private ICommonService commonService;

	/**
	 * 
	 * @Title: toStockTaking
	 * @Description: 跳转到库存盘点模块页面
	 * @date: 2014年9月2日
	 * @return:ModelAndView
	 * @throws RDPException
	 */
	@RequestMapping("toStockTaking")
	public ModelAndView toStockTaking() throws RDPException {
		ModelAndView mav = new ModelAndView("stock/stock_taking");
		UserInfo userInfo = UserUtil.getUserFromSession();
		if (userInfo != null) {
			String brandId = userInfo.getBranId();
			List<GblSecBranEntity> branList = null;
			if (brandId.toUpperCase().indexOf("ZB") != -1)// 总部
			{
				branList = commonService.getBranListForStock(null);
				mav.addObject("branList", branList);
				for (GblSecBranEntity gblSecBranEntity : branList) {
					brandId = gblSecBranEntity.getPkBranId();
					if (brandId.toUpperCase().indexOf("WX") != -1) {
						mav.addObject("branId", brandId);
					}
				}
			} else {
				branList = commonService.getBranListForStock(brandId);
				mav.addObject("branId", brandId);
				mav.addObject("branList", branList);
			}
		}
		return mav;
	}

	/**
	 * @Title: findScmTakeStk
	 * @Description: 库存盘点 从库存盘点记录表中获取分页数据
	 * @param scmTakeStk
	 * @throws RDPException
	 * @date: 2014年9月1日
	 * @return:List<ScmTakeStkEntity>
	 * @throws ParseException
	 */
	@RequestMapping(value = "findScmTakeStk", method = RequestMethod.POST)
	@ResponseBody
	public ScmTakeStkEntity findScmTakeStk(
			@RequestParam(value = "startSearchTime", required = false) String startSearchTime,
			@RequestParam(value = "endSearchTime", required = false) String endSearchTime,
			ScmTakeStkEntity scmTakeStk) throws RDPException, ParseException {
		return iStockTakingService.findScmTakeStk(startSearchTime,
				endSearchTime, scmTakeStk);
	}

	/**
	 * 
	 * @Title: delStockTake
	 * @Description: 库存盘点单删除
	 * @param scmTakeStk
	 *            库存盘点记录表
	 * @date: 2014年9月2日
	 * @return:String
	 */
	@RequestMapping(value = "delStockTake", method = RequestMethod.POST)
	@ResponseBody
	public String delStockTake(ScmTakeStkEntity scmTakeStk) {
		try {
			iStockTakingService.delScmTakeStk(scmTakeStk);
			return Constants.SUCCESS;
		} catch (RDPException e) {
			return Constants.FAILURE;
		}
	}

	/**
	 * 
	 * @Title: toStockAddStockTaking
	 * @Description: 点击开始盘点跳转到盘点商品页面; 并创建盘点单
	 * @param session
	 * @return
	 * @date: 2014年9月3日
	 * @return:ModelAndView
	 */
	@RequestMapping("toStockAddStockTaking")
	public ModelAndView toStockAddStockTaking() {
		UserInfo userInfo = UserUtil.getUserFromSession();
		// 盘点单id
		String takeId = PrimaryKeyGenerator.getPrimaryKey(BusinessType.PD,
				userInfo.getAbbrCode());
		ModelAndView mav = new ModelAndView("stock/stock_addStockTaking");
		String createTime = new SimpleDateFormat("yyyy-MM-dd")
				.format(new Date());
		mav.addObject("createTime", createTime);
		mav.addObject("takeId", takeId);
		mav.addObject("branName", userInfo.getBranName());
		mav.addObject("userName", userInfo.getName());
		ScmTakeStkEntity scmTakeStk = new ScmTakeStkEntity();
		try {
			scmTakeStk.setPkTskId(takeId);
			scmTakeStk.setFkBranId(userInfo.getBranId());
			scmTakeStk.setTakeBy(userInfo.getUserId());
			iStockTakingService.insertScmTakeStk(scmTakeStk);
		} catch (RDPException e) {
			e.printStackTrace();
		}
		return mav;
	}

	/**
	 * 
	 * @Title: doTakeStock
	 * @Description: 这里用一句话描述这个方法的作用
	 * @param takestkContent
	 *            包含商品编号 库存 盘点数量 备注信息
	 * @param fkTskId
	 *            当前盘点单id
	 * @throws RDPException
	 * @date: 2014年9月3日
	 * @return: String
	 */
	@RequestMapping(value = "doTakeStock", method = RequestMethod.POST)
	@ResponseBody
	public String doTakeStock(String takestkContent, String fkTskId) {
		try {
			iStockTakingService.doTakeStock(takestkContent, fkTskId);
			return Constants.SUCCESS;
		} catch (RDPException e) {
			return Constants.FAILURE;
		}
	}

	/**
	 * 根据盘点id 查询已盘点的商品项
	 * 
	 * @param fkTskId
	 * @return int
	 * @throws RDPException
	 */
	@RequestMapping(value = "findTakestkPrdCount", method = RequestMethod.POST)
	@ResponseBody
	public int findTakestkPrdCount(
			@RequestParam(value = "fkTskId") String fkTskId)
			throws RDPException {
		return iStockTakingService.findTakestkPrdCount(fkTskId);
	}

	/**
	 * 跳转到盘点清单页面
	 * 
	 * @param pkTskId
	 * @return ModelAndView
	 * @throws RDPException
	 */
	@RequestMapping("toStockTakingDetail")
	public ModelAndView toStockTakingDetail(
			@RequestParam(value = "pkTskId") String pkTskId)
			throws RDPException {
		ScmTakeStkEntity scmTakeStk = iStockTakingService
				.findScmTakeStkForDetail(pkTskId);
		String takeTime = new SimpleDateFormat("yyyy-MM-dd").format(scmTakeStk
				.getTakeTime());
		ModelAndView mav = new ModelAndView("stock/stock_takingDetail");
		mav.addObject("scmTakeStk", scmTakeStk);
		mav.addObject("takeTime", takeTime);
		return mav;
	}
}
